package com.zhhtech.zhh.ui.fragment

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.itxca.spannablex.spannable
import com.kingja.loadsir.core.LoadService
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.CaseConfig
import com.zhhtech.zhh.bean.HomeCaseBean
import com.zhhtech.zhh.common.checkLogin
import com.zhhtech.zhh.databinding.FragmentHomeBinding
import com.zhhtech.zhh.databinding.ItemHomeCaseBinding
import com.zhhtech.zhh.ui.activity.main.CaseDetailActivity
import com.zhhtech.zhh.ui.activity.main.CaseProgressActivity
import com.zhhtech.zhh.ui.activity.main.SchemeSubmitActivity
import com.zhhtech.zhh.ui.adapter.SyBannerImageAdapter
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseFragment
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.dialog.WeComDialog
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.helper.jumpWx
import com.zhhtech.zhh.ui.helper.loadsir.LoadSirHelper
import com.zhhtech.zhh.ui.utils.FullyGridLayoutManager
import com.zhhtech.zhh.ui.utils.FullyLinearLayoutManager
import com.zhhtech.zhh.ui.utils.SPUtils
import com.zhhtech.zhh.ui.viewmodel.MainViewModel

@SuppressLint("NotifyDataSetChanged")
class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    private val vm: MainViewModel by activityViewModels()
    private var mCaseConfig: CaseConfig? = null
    private lateinit var mLoadService: LoadService<*>
    private var mWeComDialog: WeComDialog? = null
    private val mHomeCaseAdapter = HomeCaseAdapter()
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        //  vb.tvWecom, vb.tv400, vb.tv1v1
        bindViewClick(vb.buttonGet, vb.llProgress)
        vb.rvAl.layoutManager = FullyLinearLayoutManager(requireContext())
        vb.rvAl.adapter = mHomeCaseAdapter
        mLoadService = LoadSirHelper.register(rootView!!, listener = ::onReload)
        mView = mLoadService.loadLayout
        vb.refreshLayout.setEnableLoadMore(false)
        vb.refreshLayout.setOnRefreshListener { onReload() }
        val scaleAnimationSet = AnimatorSet()
        val scaleY = ObjectAnimator.ofFloat(vb.buttonGet, "scaleY", 1f, 0.9f, 1f)
        val scaleX = ObjectAnimator.ofFloat(vb.buttonGet, "scaleX", 1f, 0.9f, 1f)
        scaleY.repeatCount = ValueAnimator.INFINITE
        scaleX.repeatCount = ValueAnimator.INFINITE
        scaleAnimationSet.interpolator = LinearInterpolator()
        scaleAnimationSet.playTogether(scaleX, scaleY)
        scaleAnimationSet.duration = 2000
        scaleAnimationSet.start()
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                scaleAnimationSet.end()
                scaleAnimationSet.cancel()
            }
        })
        if (SPUtils.getBoolean("key_first", true)) {
            startActivityExt<SchemeSubmitActivity>()
            SPUtils.put("key_first", false)
        }
        onReload()
    }

    private fun onReload() {
        launchUI({
            val honeBean = api.home().toData()
            mHomeCaseAdapter.datas = honeBean?.caseList ?: mutableListOf()
            mHomeCaseAdapter.notifyDataSetChanged()
            mCaseConfig = api.indexConfig().toData()
            vm.mConfig.value = mCaseConfig
        }, ApiObserver(context, loadService = mLoadService, refreshLayout = vb.refreshLayout))
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonGet -> checkLogin { startActivityExt<SchemeSubmitActivity>() }
//            vb.tvWecom -> {
//                jumpWx(mCaseConfig)
//            }
//            vb.tvWecom -> mCaseConfig?.weCom.clipboard()
//            vb.tv400 -> startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:${mCaseConfig?.phone}")))
//            vb.tv1v1 -> startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:${mCaseConfig?.contactNumber}")))
            vb.llProgress -> startActivityExt<CaseProgressActivity>()
        }
    }
}

class HomeCaseAdapter : BaseRecyclerAdapter<ItemHomeCaseBinding, HomeCaseBean>() {
    override fun onBindViewHolder(vb: ItemHomeCaseBinding, bean: HomeCaseBean, position: Int) {
//        vb.tvType.text = bean.loanType
        vb.tvUser.text = bean.username
        vb.tvWork.text = bean.work
        vb.tvInfo4.text = bean.loanType
        vb.tvInfo5.text = bean.money
        vb.tvInfo6.text = bean.solution
        setViewClick(position, vb.root)
    }

    override fun subOnClickAdapter(v: View, position: Int) {
        v.context.startActivityExt<CaseDetailActivity> {
            putExtra("position", "home")
            putExtra("case", getItem(position))
        }
    }
}